package com.smart.stock.module.maindata.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.smart.stock.module.maindata.services.StockCodeInfoService;
import com.smart.stock.module.maindata.services.StockDailyIndexesService;
import com.smart.stock.module.maindata.services.StockDailyPriceServices;
import com.smart.stock.module.maindata.services.StockService;
import com.smart.stock.module.maindata.vo.StockVo;
import com.smart.stock.module.spider.jobSpiderApi.XueQiuApi;
import com.smart.stock.module.spider.util.TradingDateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


/**
 * @author chenguoxiang
 * @create 2018-11-02 15:49
 **/
@Api(tags = "股票信息")
@Slf4j
@Controller
@RequestMapping("/stockInfo")
public class StockInfoController {

    @Autowired
    private StockService service;

    @Autowired
    private StockCodeInfoService stockCodeInfoService;
    @Autowired
    private StockDailyPriceServices stockDailyPriceServices;
    @Autowired
    private TradingDateUtil tradingDateUtil;
    @Autowired
    private XueQiuApi xueQiuApi;
    @Autowired
    private MongoTemplate template;
    @Autowired
    private StockDailyIndexesService stockDailyIndexesService;


    @ApiOperation(value = "获取代码列表", notes = "根据传入参数返回列表信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "显示条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "code", value = "代码、名称", required = false, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "type", value = "市场类别", required = false, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "pb", value = "市净值", required = false, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "dy", value = "股息", required = false, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "industry", value = "所属行业", required = false, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", required = false, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "sord", value = "排序方式", required = false, paramType = "query", dataType = "string"),
    })
    @ResponseBody
    @GetMapping("/list")
    public String queryStock(Integer page,
                             Integer rows,
                             String code,
                             String type,
                             String pb,
                             String dy,
                             String industry,
                             String typeFilter,
                             String priceFilter,
                             String maybeFilter,
                             String maybeProfitFilter,
                             String maybeLossFilter,
                             String adjustBackFilter,
                             String totalMarketValueFilter,
                             String volReducedRatioFilter,
                             String lowestVolFilter,
                             String thsIndustry,
                             String thsConcepts,
                             String mainBusiness,
                             String sidx,
                             String sord) {
        String order = (null == sord || sord.isEmpty()) ? "asc" : sord;
        String field = (null == sidx || sidx.isEmpty()) ? "_id" : sidx;
        String str = service.queryStock(page,
                rows, code, type, pb, dy, industry,
                typeFilter, priceFilter, maybeFilter,
                maybeProfitFilter,maybeLossFilter,
                adjustBackFilter,
                totalMarketValueFilter,
                volReducedRatioFilter,
                lowestVolFilter,
                thsIndustry,
                thsConcepts,
                mainBusiness,
                field,
                order);
        return str;
    }


    @ApiOperation(value = "stockInfo")
    @RequestMapping("/{code}")
    public String stockInfo(@PathVariable String code, Model model) {
        JSONObject json= stockCodeInfoService.getStockInfo(code);
        StockVo stockVo = JSON.toJavaObject(json,StockVo.class);
        model.addAttribute("pagetitle",String.format("%s-%s",code,stockVo.getName()));
        model.addAttribute("data",json.toJSONString());
        model.addAttribute("stock", JSON.toJSONString(stockVo));
        model.addAttribute("holds", JSON.toJSONString(xueQiuApi.getHolders(code)));
        return "/stock/info/stock_info";
    }


}
